// ==UserScript==
// @name         ChatGPT Improved Prompt Starters Handler with Enhanced UI
// @namespace    http://tampermonkey.net/
// @version      2.0
// @match        https://chat.openai.com/**
// @grant        none
// @require      https://unpkg.com/@popperjs/core@2
// @require      https://unpkg.com/tippy.js@6
// ==/UserScript==

(function() {
    'use strict';

    let promptStarters = [];
    let tippyInstance = null; // 用于存储Tippy.js实例

    const originalFetch = window.fetch;
    window.fetch = function(url, config) {
        return originalFetch.apply(this, arguments)
            .then(response => {
                if (url.includes('g-x0oFy6twt-code-insight-enhancer')) {
                    const responseClone = response.clone();
                    responseClone.json().then(json => {
                        if (json && json.gizmo && json.gizmo.display && json.gizmo.display.prompt_starters) {
                            promptStarters = json.gizmo.display.prompt_starters;
                            initializeTippy();
                        }
                    });
                }
                return response;
            });
    };

    function initializeTippy() {
        const targetElement = document.querySelector('form > div > div.flex.w-full.items-center > div > div');
        if (!targetElement) {
            console.error('Target element not found');
            return;
        }

        tippyInstance = tippy(targetElement, {
            content: 'Loading...',
            allowHTML: true,
            placement: 'top',
            interactive: true,
            theme: 'dark', // 使用暗色主题
            maxWidth: 500,
            appendTo: document.body,
            onShow(instance) {
                instance.setContent(createTippyContent(instance));
            }
        });
    }

//     function createTippyContent(tippyInstance) {
//         const container = document.createElement('div');
//         container.style.display = 'flex';  // 使用flex布局
//         container.style.flexWrap = 'wrap'; // 允许内容换行
//         container.style.maxWidth = "500px", // 动态设置最大宽度
//         container.style.color = 'white';

//         promptStarters.forEach((item, index) => {
//             const itemElement = document.createElement('div');
//             itemElement.textContent = `${index + 1}. ${item}`;
//             itemElement.className = 'tippy-item';
//             itemElement.style.cursor = 'pointer';
//             itemElement.style.padding = '5px';
//             itemElement.style.flexBasis = '50%'; // 每个项占据50%的宽度
//             itemElement.addEventListener('click', () => {
//                 document.querySelector('#prompt-textarea').value += item;

//                 const button = document.querySelector('form > div > div.flex.w-full.items-center > div > button');
//                 if (button) {
//                     button.disabled = false;
//                 }

//                 if (tippyInstance) {
//                     tippyInstance.hide();
//                 }
//             });
//             container.appendChild(itemElement);
//         });
//         return container;
//     }

    function createTippyContent() {
        const container = document.createElement('div');
        container.style.color = 'white';
        promptStarters.forEach((item, index) => {
            const itemElement = document.createElement('div');
            itemElement.textContent = `${index + 1}. ${item}`;
            itemElement.className = 'tippy-item';
            itemElement.style.cursor = 'pointer';
            itemElement.style.padding = '5px';
            itemElement.addEventListener('click', () => {
                // 添加内容到textarea
                const textarea = document.querySelector('#prompt-textarea');
                textarea.value += item;

                // 激活按钮
                const button = document.querySelector('form > div > div.flex.w-full.items-center > div > button');
                if (button) {
                    button.disabled = false; // 启用按钮
                    button.click(); // 如果需要的话，也可以模拟点击按钮
                }

                // 隐藏悬浮窗
                if (tippyInstance) {
                    tippyInstance.hide(); // 正确隐藏悬浮窗
                }
            });
            container.appendChild(itemElement);
        });
        return container;
    }

    // 添加自定义样式
    const styleEl = document.createElement('style');
    styleEl.textContent = `
        .tippy-box[data-theme~='dark'] {
            background-color: black; /* 设置暗色背景 */
            color: white;
        }
        .tippy-item:hover {
            background-color: #333; /* 鼠标悬停时的背景色 */
        }
    `;
    document.head.appendChild(styleEl);
})();
